﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace IISLogReader
{
    public class LogReader:IDisposable
    {
        private string _filePath;
        private LogHeader _header;

        public LogHeader Header
        {
            get { return _header; }
            set { _header = value; }
        }
        public string FilePath
        {
            get { return _filePath; }
            set { _filePath = value; }
        }
        private List<LogField> _fields = new List<LogField>();
        private StreamReader _streamReader = null;
        public LogReader(string filePath)
        {
            _filePath = filePath;
            _header = new LogHeader(new FileInfo(filePath));
            OpenInput(filePath);            
        }

        private void OpenInput(string filePath)
        {
            _streamReader = File.OpenText(filePath);
            GatherFileStructure();
        }

        private string GatherFileStructure()
        {
            //TODO: Populate other metadata/reader properties found in first 4 rows
            string line = _streamReader.ReadLine();
            //TODO: reading one more line lost the first one think through the 
            while (line.StartsWith("#"))//all meta data lines start with #
            {
                if (line.Contains("Fields"))
                {
                    string[] nameValueP = line.Split(':');
                    if (nameValueP.Length > 1)
                    {
                        string[] fieldNames = nameValueP[1].Trim().Split(' ');//after colon space separated filed name list
                        int z = 0;
                        _fields = new List<LogField>();// initialize again as it could be filled earlier
                        foreach (string fieldName in fieldNames)
                        {
                            z++;
                            _fields.Add(new LogField(fieldName, z));
                        }
                    }
                    break;
                }
                line = _streamReader.ReadLine();
            }
            if (_fields.Count < 1)
            {
                throw new FormatException("File format is not recognised.");
            }
            return line;
        }
        public LogRow ReadRow() 
        {
            string line = _streamReader.ReadLine().Trim();
            string [] columns = null;
            //if in a middle of the file headers are found again 
            //initialize fields again and rewind until data is found.
            if (line.StartsWith("#"))
            {
                // advance and re-initialize the line
                line = GatherFileStructure();
                // if the cursor still sits on row starting with #
                if (line.StartsWith("#"))
                {
                    line = _streamReader.ReadLine().Trim();
                }
            }
            
            columns = line.Split(' ');
            return new LogRow(_fields, columns);
        }
        public LogRowTyped Read()
        {
            return new LogRowTyped(ReadRow(),_header);
        }
        public bool IsEOF 
        { 
            get 
            { 
                return _streamReader.EndOfStream; 
            } 
        }

        #region IDisposable Members

        public void Dispose()
        {
            _streamReader.Close();
            _streamReader.Dispose();
        }

        #endregion
    }
    
}
